package com.wechat.servlet;

import com.wechat.DAO.DaoImpl.UserInfoDAOIpml;
import com.wechat.DAO.UserInfoDAO;
import com.wechat.servlet.Utils.Base64Decoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AlterInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String key = request.getParameter("key");
        String value = request.getParameter("value");
        String username = request.getParameter("username");
        System.out.println(value);
        String realValue = Base64Decoder.decode(value.replace(" ","+"));
        UserInfoDAO userInfoDAO = new UserInfoDAOIpml();
        try {
            switch (key){
                case "nickname":{
                    if(userInfoDAO.updateNickname(realValue,username)){
                        response.getWriter().write("success");
                    }else {
                        response.getWriter().write("fail");
                    }
                }
                break;

                case "HPUrl":{
                    if(userInfoDAO.updateHPUrl(realValue,username)){
                        response.getWriter().write("success");
                    }else {
                        response.getWriter().write("fail");
                    }
                }
                break;

                case "country":{
                    if(userInfoDAO.updateCountry(realValue,username)){
                        response.getWriter().write("success");
                    }else {
                        response.getWriter().write("fail");
                    }
                }
                break;

                case "province":{
                    if(userInfoDAO.updateProvince(realValue,username)){
                        response.getWriter().write("success");
                    }else {
                        response.getWriter().write("fail");
                    }
                }
                break;

                case "city":{
                    if(userInfoDAO.updateCity(realValue,username)){
                        response.getWriter().write("success");
                    }else {
                        response.getWriter().write("fail");
                    }
                }
                break;

                case "sex":{
                    if(userInfoDAO.updateSex(realValue,username)){
                        response.getWriter().write("success");
                    }else {
                        response.getWriter().write("fail");
                    }
                }
                break;

            }

        }catch (Exception e){
            e.printStackTrace();
            response.getWriter().write("fail");
            return;
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
